
#### predictors of topic proportions


# to manipulate data
library(tidyverse)

# to cluster standard errors
library(lmtest)
library(sandwich)

# to generate results tables
library(stargazer)



# load data
load("full_data_conditionality_covariates.RData")


#### SUBSET DATA 
ols_df <- full_df %>%
  select(iso3c,year,resource_conditionality,resource_conditionality_dict_tfidf,imf_program_new,
         resource_rents,log_gdp_pc_constant,gdp_growth,field_discovery,
         oil_price,crisis,polity2,left_executive,election,any_war,ideal_point_usa,eiti_member,log_external_debt_stocks,
         nationalization_oc,wb_extractive_project,working_age_pop,poverty_headcount,t) %>%
  mutate(resource_conditionality = resource_conditionality * 100,
         ideal_point_usa = ideal_point_usa*(-1)) %>%
  group_by(iso3c) %>%
  arrange(year, .by_group = T) %>%
  mutate(across(resource_rents:poverty_headcount, list(lag = ~ lag(.,1)))) %>%
  ungroup() %>%
  filter(year>1979 & imf_program_new == 1) # only country-years of IMF program onset




ols1 <- lm(resource_conditionality ~ log_gdp_pc_constant_lag + gdp_growth_lag + resource_rents_lag + #log_external_debt_stocks_lag +
             working_age_pop_lag + field_discovery_lag + oil_price_lag + crisis_lag + wb_extractive_project_lag +
             polity2_lag + left_executive_lag + 
             election_lag + nationalization_oc_lag + 
             any_war_lag + eiti_member_lag + ideal_point_usa_lag +
                            as.factor(iso3c) + as.factor(year),
                          data = ols_df)

ols1_r <- ols1 %>% 
  coeftest(vcovHC(ols1, type = 'HC0', cluster = c('iso3c', 'Summary')))


ols2 <- lm(resource_conditionality_dict_tfidf ~ log_gdp_pc_constant_lag + gdp_growth_lag + resource_rents_lag + #log_external_debt_stocks_lag +
             working_age_pop_lag + field_discovery_lag + oil_price_lag + crisis_lag + wb_extractive_project_lag +
             polity2_lag + left_executive_lag + 
             election_lag + nationalization_oc_lag + 
             any_war_lag + eiti_member_lag + ideal_point_usa_lag +
             as.factor(iso3c) + as.factor(year),
           data = ols_df)

ols2_r <- ols2 %>% 
  coeftest(vcovHC(ols2, type = 'HC0', cluster = c('iso3c', 'Summary')))

# TABLE E.1 (with clustered standard errors)
stargazer(ols1_r,ols2_r, omit=c("iso3c","year"), #type="text",
          dep.var.labels = c("Resource Conditionality (Topic %)","Resource Conditionality (TF--IDF)"),
          covariate.labels = c("GDP per Capita (Log)",
                               "GDP Growth (%)",
                               "Resource Rents (% GDP)",
                               "Working Age Population (%)",
                               "Field Discovery = 1",
                               "Oil Price (USD)",
                               "Crisis = 1",
                               "WB Extractive Project",
                               "Democracy (Polity2)",
                               "Left Executive = 1",
                               "Election Year = 1",
                               "Oil Company Nationalization = 1",
                               "War = 1",
                               "EITI Member = 1",
                               "Voting with US"),
          no.space = T)

# retrieve n, r2 etc
stargazer(ols1,ols2, omit=c("iso3c","year"), #type="text",
          dep.var.labels = c("Resource Conditionality (Topic %)","Resource Conditionality (TF--IDF)"),
          covariate.labels = c("GDP per Capita (Log)",
                               "GDP Growth (%)",
                               "Resource Rents (% GDP)",
                               "Working Age Population (%)",
                               "Field Discovery = 1",
                               "Oil Price (USD)",
                               "Crisis = 1",
                               "WB Extractive Project",
                               "Democracy (Polity2)",
                               "Left Executive = 1",
                               "Election Year = 1",
                               "Oil Company Nationalization = 1",
                               "War = 1",
                               "EITI Member = 1",
                               "Voting with US"),
          no.space = T)

